using System;
using System.Collections;
using System.Web;

namespace Sedna.Core.Controller.Common
{
	/// <summary>
	/// Summary description for SetupExecutionContextParameterAction.
	/// </summary>
	public class SetupExecutionContextParameterAction : AbstractAction
	{
		private bool ignoreIfEmpty = false;
		private bool setForCurrentPrefix = false;

		public bool IgnoreIfEmpty
		{
			set { ignoreIfEmpty = value; }
		}

		public bool SetForCurrentPrefix
		{
			set { setForCurrentPrefix = value; }
		}


		public override IActionResult Execute(IExecutionContext executionContext)
		{
			if(ActionParams == null || ActionParams.Count == 0)
			{
				return CreateResult(successStatus);
			}

			foreach(IActionParam param in actionParams)
			{
				string value = param.GetValue(executionContext);
				if(ignoreIfEmpty && (value == null || string.Empty == value))
				{
					continue;
				}
				if(setForCurrentPrefix)
				{
					executionContext.SetToCurrentScope(param.Name, value);
				}
				else
				{
					executionContext[param.Name] = value;
				}
			}

			return CreateResult(successStatus);
		}
	}
}
